package com.dj.dormitory.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dj.dormitory.commonutils.Result;
import com.dj.dormitory.entity.Grade;
import com.dj.dormitory.service.GradeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author DJ同学
 * @since 2021-01-24
 */
@RestController
@RequestMapping("/grade")
public class GradeController {

    @Autowired
    private GradeService gradeService;

    @RequestMapping("/partList")
    public Result getPartList(@RequestBody Map map){
        Page<Grade> page  = gradeService.getPartList(map);
        return Result.ok().data("grades",page);
    }
    @RequestMapping("/list")
    public Result getList(){
        List<Grade> list = gradeService.list();
        return Result.ok().data("grades",list);
    }
    @PostMapping("/add")
    public Result add(@RequestBody Grade grade){
        grade.setId(Integer.parseInt(grade.getName()));
        try{
            boolean save = gradeService.save(grade);
            return save ? Result.ok().message("添加年级成功"):Result.error().message("添加年级失败");
        }catch (Exception e){
            e.printStackTrace();
            return Result.error().message("添加年级失败");
        }
    }
//    未启用
    @PostMapping("/edit")
    public Result editGrade(@RequestBody Grade grade){
        boolean edit = gradeService.updateById(grade);
        return edit ? Result.ok():Result.error();
    }
    @PostMapping("deleteOne/{id}")
    public Result deleteOne(@PathVariable(value = "id") Integer id){
        boolean delete = gradeService.removeById(id);
        return delete ? Result.ok().message("删除年级成功"):Result.error().message("删除年级失败");
    }
    @PostMapping("/deletePart/{ids}")
    public Result deletePart(@PathVariable("ids") List<String> ids){
        boolean delete = gradeService.removeByIds(ids);
        return delete ? Result.ok().message("批量删除年级成功"):Result.error().message("批量删除年级失败");
    }
}

